<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Line;
class LineController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    //
    /*
     * 录入
     */
	public function create(Request $request) {
    	$line = Line::create($request->all());
//     	$user = $request;
    	return response()->json($line);
    }
    
    //获取服务器线路列表
    public function get(Request $request) {
    	
    	$where = array();
    	if ($request->input('province')){
    		$province = $request->input('province');
    		$where['province'] = $province;
    	}
    	if ($request->input('city')){
    		$city = $request->input('city');
    		$where['city'] = $city;
    	}
    	if ($request->input('level')){
    		$level = $request->input('level');
    		$where['level'] = $level;
    	}
    	if ($request->input('carrieroperator')){
    		$carrieroperator = $request->input('carrieroperator');
    		$where['carrieroperator'] = $carrieroperator;
    	}
    	
    	$where['enable_flag'] = 1;
    	
    	$lines = Line::where($where)->get();
    	
    	$return = array(
    			'code' => 0,
    			'msg' => 'OK',
    			'data' => array(
    					'lines'=>$lines
    			)
    	);
        return response()->json($return); 
    	
    }



	public function getlines(Request $request) {

		$where = array();
		if ($request->input('province')){
			$province = $request->input('province');
			$where['province'] = $province;
		}
		if ($request->input('city')){
			$city = $request->input('city');
			$where['city'] = $city;
		}
		if ($request->input('level')){
			$level = $request->input('level');
			$where['level'] = $level;
		}
		if ($request->input('carrieroperator')){
			$carrieroperator = $request->input('carrieroperator');
			$where['carrieroperator'] = $carrieroperator;
		}

//		$where['enable_flag'] = 1;

		$lines = Line::where($where)->where('enable_flag','>',0)->get();

		$return = array(
			'code' => 0,
			'msg' => 'OK',
			'data' => array(
				'lines'=>$lines
			)
		);
		return response()->json($return);

	}


	/**
	 * @param Request $request
	 * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
	 * 客服数据
	 */
	public function getServiceLines(Request $request)
	{

		if(empty($request->input('terminal')) || !$request->has('terminal') || md5('niumokeji') != $request->input('terminal') )
		{
			$return = array(
				'code' => -1,
				'msg' => '信息有误',
				'data' => array()
			);
			return response()->json($return);

		}

		$where = array();
		if ($request->input('province')){
			$province = $request->input('province');
			$where['province'] = $province;
		}
		if ($request->input('city')){
			$city = $request->input('city');
			$where['city'] = $city;
		}
		if ($request->input('level')){
			$level = $request->input('level');
			$where['level'] = $level;
		}
		if ($request->input('carrieroperator')){
			$carrieroperator = $request->input('carrieroperator');
			$where['carrieroperator'] = $carrieroperator;
		}


		$lines = Line::where($where)->whereIn('enable_flag',['1','2'])->get(['province','city','carrieroperator','ip as service_address']);

		$return = array(
			'code' => 0,
			'msg' => 'OK',
			'data' => array(
				'lines'=>$lines
			)
		);
		return response()->json($return);
	}


	/**
	 * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
	 * 获取线路解析域名列表
	 */
	public function getLinesIpAddress()
	{
		$lines = Line::where('enable_flag','>',0)->get();

		$ip = [];
		foreach($lines as $key=>$line)
		{
			$service_ip =  gethostbyname($line->ip);
			if($service_ip == $line->ip)
			{
				continue;
			}
			$ip[] .= $service_ip;
		}


		$return = array(
			'code' => 0,
			'msg' => 'OK',
			'data' =>$ip
		);
		return response()->json($return);

	}




}
